﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using CMN.Controllers;
using CMN.Controllers.Filters;
using CMN.Common;
using System.Text;
using CMN.Framework;
using System.Drawing;
using System.IO;
using Newtonsoft.Json;

namespace CMN.Controllers
{
    [UserAuthorize]
    [LoggerFilter()]
    [ExceptionFilter()]
    public class UserController : Controller
    {
        // 返回json字符串
        protected string returnJson = string.Empty;
        public ActionResult Index()
        {
            if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
            {

                 Response.Redirect("~/manager/home/index");
                 return null;
            }
            else
            {
                return View("login");//页面地址
            }
        }

        public string Test(string actionId)
        {
            System.IO.StreamReader strReader = new StreamReader(Request.InputStream);
            string str = strReader.ReadToEnd();
          //  string[] allKey =this.HttpContext.Request.Form.AllKeys;
            //string str = this.Request["JSON"].ToString();
            List<Sys_RolesEntity> roles = (List<Sys_RolesEntity>)JsonConvert.DeserializeObject(str, typeof(List<Sys_RolesEntity>));
            return "";
        }

        public string Login()
        {
            
           string userName=  (string)Utils.GetQueryAndFormString("username", this, 20, 3, DataType.CharAndNum);
           string pwd = (String)Utils.GetQueryAndFormString("userpwd", this, 20, 1, DataType.CharAndNum);
           string checkCode = (string)Utils.GetQueryAndFormString("CheckCode", this, 20, 0, DataType.CharAndNum);
           if (Session["CheckCode"]==null)
           {
               return returnJson = string.Concat("{success:\"false\",message:\"请刷新验证码，验证码已超时！！\"}");
           }
           if (checkCode.ToLower() != Session["CheckCode"].ToString().ToLower())
           {
              return returnJson = string.Concat("{success:\"false\",message:\"验证码不正确！！\"}");
           }
           string userKey=Utils.GetIP().Replace(".", "");
           if (!UserLogin.CheckDisableLoginUser(userKey))
           {
                returnJson = string.Concat("{success:\"false\",message:\"此用户:",userName,",IP:",userKey,"登陆出错次数超过系统允许,请10分钟后在登录！！\"}" );
           }
           else if(new UserLogin().CheckLogin(userName, pwd, userKey))
           {
               UserLogin.MoveErrorLoginUser(userKey);
               returnJson = "{success:\"true\"}";
           }
           else //用户名 ，密码错误
           {
               returnJson = "{success:\"false\",message:\"用户名或密码错误！\"}";
               Session["CheckCode"] = null;
           }
            return returnJson;
        }
        /// <summary>
        /// 返回验证码
        /// </summary>
        /// <returns></returns>
        public Image GetCheckCode()
        {
           

           string checkCode = Utils.RndChar(4);
           ImageHelper imgHelper = new ImageHelper();
           System.Drawing.Image image = imgHelper.CreateCheckCodeImage(checkCode, 138, 50, 28);
           System.IO.MemoryStream ms = new System.IO.MemoryStream();
           image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
           image.Dispose();
           Response.ClearContent();
           Response.ContentType = "image/x-png";
           Response.BinaryWrite(ms.ToArray());
           ms.Dispose();
           Session["CheckCode"] = checkCode;
           return image;
        }
        public string Logout()
        {
            UserLogin.UserOut();
            Response.Redirect("~/manager/user/index");
            return returnJson;
        }

        
    }
}
